import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableOpacity,

} from 'react-native';
/*connect 连接 React 组件与 Redux store。
    返回一个新的已与 Redux store 连接的组件类。
 */
import {connect} from 'react-redux';
//router界面跳转
import {Actions} from 'react-native-router-flux'

//引入相关的action
import {loginIn} from '../../ReduxFile/Actions';

class Login extends Component {
    constructor(props) {
        super(props);
        console.log('constructor props:'+props.loginState);
        this.state = {
            loginStatus: props.status,
        }
    }
    componentWillReceiveProps(nextProps) {
        console.log('loginInProps: ' + nextProps.loginState);
        this.setState({
            loginStatus: nextProps.loginState,
        })
        switch (nextProps.loginState){
            case 'login_finish': {
                Actions.root({});
            }
        }
    }
    render() {
        return (
            <View style={styles.container}>
                <TouchableOpacity onPress={()=>this._onLogin()} style={{backgroundColor:'orange',width:200,height:40,padding:5}}>
                    <Text style={styles.welcome}>
                        LoginButton
                    </Text>
                </TouchableOpacity>
                <Text style={styles.instructions}>
                    To {this.state.loginStatus}
                </Text>
                <Text style={styles.instructions}>
                    Double tap R on your keyboard to reload,{'\n'}
                    Shake or press menu button for dev menu
                </Text>
            </View>
        );
    }
    _onLogin=()=>{
        this.props.dispatch(loginIn());
    }
}

const loginStoreState = (store)=> {
    console.log('loginStoreState: '+store);
    return {
        loginState: store.Login.status,
    }
}
export default Login = connect(loginStoreState)(Login)

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 14,
        textAlign: 'center',
        margin: 10,
        color:'black',
        fontWeight:'bold',
    },
    instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
    },
});